package com.g13.virtualpethospital.service.Implement;

import com.g13.virtualpethospital.entity.QuestionEntity;
import com.g13.virtualpethospital.entity.TestPaperBriefEntity;
import com.g13.virtualpethospital.exceptions.QuestionException;
import com.g13.virtualpethospital.service.QuestionService;
import com.g13.virtualpethospital.service.TestExamService;
import com.g13.virtualpethospital.service.TestPaperService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Title: QuestionServiceImpl
 * @Author yyj1205
 * @Package com.g13.virtualpethospital.service.Implement
 * @Date 24/3/2024 23:13
 * @description: UserService的一个实现
 */
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    TestPaperService testPaperService;
    @Override
    public void addQuestion(QuestionEntity question) {
        question.setQuestionId(null);
        mongoTemplate.insert(question);
    }

    @Override
    public void updateQuestion(QuestionEntity question, String id) throws QuestionException {
        QuestionEntity question1 = mongoTemplate.findById(id, QuestionEntity.class);
        if(question1 == null) throw new QuestionException(QuestionException.QuestionErrorCode.FIND_NO_QUEST);
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        if(question.getAnswer() != null)
        update.set("answer", question.getAnswer());
        if(question.getContent() != null)
        update.set("content", question.getContent());
        if(question.getOptions() != null)
        update.set("Options", question.getOptions());
        if(question.getQuestionLevel1() != null)
        update.set("questionLevel1", question.getQuestionLevel1());
        if (question.getQuestionLevel2() != null)
        update.set("questionLevel2", question.getQuestionLevel2());
        UpdateResult result = mongoTemplate.upsert(query,update, QuestionEntity.class);
    }

    @Override
    public void deleteQuestion(String id) throws QuestionException {
        QuestionEntity question = mongoTemplate.findById(id, QuestionEntity.class);
        if(question == null) throw new QuestionException(QuestionException.QuestionErrorCode.FIND_NO_QUEST);
        if(question.getUsedCnt() > 0) throw new QuestionException(QuestionException.QuestionErrorCode.USED_QUESTION);
        Query query = new Query(Criteria.where("_id").is(id));
        DeleteResult result = mongoTemplate.remove(query, QuestionEntity.class);
        if(result.getDeletedCount() == 0) throw new QuestionException(QuestionException.QuestionErrorCode.DEL_FAIL);

    }

    @Override
    public List<QuestionEntity> findQuestionByType(String menu, String subMenu) {
        Query query = null;
        if((menu == "" || menu == null) && (subMenu == "" || subMenu == null)){
            return findAllQuestion();
        } else if (menu == "" || menu == null) {
            query = new Query(Criteria.where("questionLevel2").is(subMenu));
        }
        else if (subMenu == "" || subMenu == null){
            query = new Query(Criteria.where("questionLevel1").is(menu));
        }
        else {
            query = new Query(Criteria.where("questionLevel1").is(menu).and("questionLevel2").is(subMenu));
        }
        return mongoTemplate.find(query, QuestionEntity.class);
    }

    @Override
    public List<QuestionEntity> findQuestionByName(String name) {
        Query query = new Query();
        Pattern pattern = Pattern.compile("^.*" + name + ".*$");
        query.addCriteria(Criteria.where("content").regex(pattern));
        return mongoTemplate.find(query, QuestionEntity.class);
    }

    @Override
    public List<QuestionEntity> findAllQuestion() {
        return mongoTemplate.findAll(QuestionEntity.class);
    }

    @Override
    public String findCorrectOptionByQuestionId(String questionId) throws QuestionException {
        QuestionEntity question = mongoTemplate.findById(questionId, QuestionEntity.class);
        if(question == null) throw new QuestionException(QuestionException.QuestionErrorCode.FIND_NO_QUEST);
        return question.getAnswer();
    }

    @Override
    public List<String> findQuestionIdsByPaperId(String paperId) throws QuestionException {

        List<TestPaperBriefEntity> testPaperBrief = testPaperService.findTestPaperById(paperId);
        if(testPaperBrief.isEmpty()) throw new QuestionException(QuestionException.QuestionErrorCode.FIND_NO_PAPER);
        List<String> strings = Arrays.asList(testPaperBrief.get(0).getQuestions());
        return strings;
    }





    @Override
    public QuestionEntity findQuestionById(String id) {
        return mongoTemplate.findById(id, QuestionEntity.class);
    }

}
